package com.guaoran.agricultural.big.data.controller;

import com.guaoran.newsdata.api.domain.News;
import com.guaoran.newsdata.api.service.NewsDataCoreService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

/**
 * @Author gucheng
 * @Description 新闻
 * 2019-07-18 11:07
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Value("${spring.cloud.news.service.application.name}")
    private String newsServiceApplicationName;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private NewsDataCoreService newsDataCoreService;

    private Logger log = LogManager.getLogger();

    /**
     * 获得所有列表，使用 {@link RestTemplate} 方式调用
     * @return
     */
    @GetMapping("/all")
    public Collection<News> findAll() {
        log.warn("all{}","ooooooo");
        return restTemplate.getForObject("http://"+newsServiceApplicationName+"/news/list",Collection.class);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/find/{id}")
    public News findOne(@PathVariable("id") Long id){
        log.warn("find {}",id);
        return newsDataCoreService.findById(id);
    }

    /**
     * 添加
     * @param news
     * @return
     */
    @PostMapping("/save")
    public boolean save(News news) {
        return newsDataCoreService.save(news);
    }

    /**
     * 修改
     * @param news
     * @return
     */
    @PutMapping("/update")
    public boolean update(News news) {
        return newsDataCoreService.update(news);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return newsDataCoreService.delete(id);
    }

}

